//
//  PKRadioHeaderView.m
//  PianKe
//
//  Created by ybq on 15/12/23.
//  Copyright © 2015年 B7_Young. All rights reserved.
//

#import "PKRadioHeaderView.h"


@interface PKRadioHeaderView () <UIScrollViewDelegate>

@property (nonatomic, assign) int num;

@end

@implementation PKRadioHeaderView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
    
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 175)];
    _scrollView.pagingEnabled = YES;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.contentSize = CGSizeMake(kWidth * 4, 175);
    _scrollView.delegate = self;
    [self addSubview:_scrollView];
    
    // 创建轮播图的imageView
    for (int i = 0; i < 4; i++) {
        UIImageView *carouselImageView = [[UIImageView alloc] initWithFrame:CGRectMake(kWidth * i, 0, kWidth, 175)];
        carouselImageView.tag = 1000 + i;
        
        // 开启交互
        carouselImageView.userInteractionEnabled = YES;
        [_scrollView addSubview:carouselImageView];
    }
    
    _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(kWidth - 70, 150, 50, 20)];
    _pageControl.numberOfPages = 3;
    [self addSubview:_pageControl];
    
    // 创建精选的3button
    for (int i = 0; i < 3; i++) {
        PKHotlistButton *hotlistButton = [[PKHotlistButton alloc] initWithFrame:CGRectMake(10 + (((kWidth - 30) / 3) + 5) * i, 185, (kWidth - 30) / 3, (kWidth - 30) / 3)];
        hotlistButton.tag = i + 100;
        [self addSubview:hotlistButton];
    }
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 305, 100, 20)];
    label.text = @"全部电台·All Radios";
    label.textColor = [UIColor lightGrayColor];
    label.font = [UIFont systemFontOfSize:10];
    [self addSubview:label];
    
    UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(110, 315, kWidth - 110, .5)];
    line.backgroundColor = [UIColor lightGrayColor];
    [self addSubview:line];
    
    NSTimer *timer =  [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(timerAction:) userInfo:@"图片轮播" repeats:YES];
    _num = 0;
    [timer fire];
    
}

// NSTimer实现
- (void)timerAction:(NSTimer *)timer {
    _pageControl.currentPage = self.num;
    [_scrollView setContentOffset:CGPointMake(kWidth * self.num, 0) animated:NO];
    self.num++;
    if (self.num == 3) {
        self.num = 0;
    }
    
}

// 实现代理方法
// 连接UIPageControl
// 图片停止减速时触发
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    // 通过偏移量的x坐标 来关联currentPage
    // 取出偏移量
    CGFloat x = scrollView.contentOffset.x;
    NSInteger num = x / kWidth;
    if (num == 3) {
        num = 0;
    }
    _pageControl.currentPage = num;
    
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    if (scrollView.contentOffset.x > kWidth * 3) {
        [scrollView setContentOffset:CGPointMake(0, 0) animated:NO];
    }
        
    if (scrollView.contentOffset.x < 0) {
        [scrollView setContentOffset:CGPointMake(kWidth * 3, 0) animated:NO];
    }

}


@end
